#! /usr/bin/perl

use strict;

use constant true => 1;
use constant false => 0;

#Sub-routine defn's
sub check_prereqs;
sub install_postgre;
sub install_python;
sub install_django;
sub log_err;

sub check_prereqs {

	#CHECK PYTHON 
	my $cmd = "dpkg-query -l python";
	my $results = `$cmd`;
	
	#If python isn't installed, install it
	if($results =~ m/No packages found matching test./){
		install_python;
	} else {
		my $version;
		#Find out python version	
		if ($results =~ /(\d)\.(\d)\.(\d)/){
			$version = "$1.$2";
		} else {
			log_err("Error checking python package\nPlease try install python on your own\n");
		}

		if($version lt "2.7" && $version gt "2.2"){
			print "Python version is compatible\nMoving on to PostgreSQL\n";
		}
	}

	print "Checking PostgreSQL...\n";
	#CHECK POSTGRESQL
	my $cmd = "dpkg-query -l postgresql";
        my $results = `$cmd`;

        #If postgresql isn't installed, install it
        if($results =~ m/No packages found matching test./){
        	install_postgre;
	} else {
		if ($results !~ /(\d)\.(\d)\.(\d)/){
                        log_err("Error with postgresSQL.  Attempting to install postgreSQL\n",false);
                } else {
			print "HERE";
		}

		#install psycopg2
		#get psycopg2 from http://initd.org/pub/software/psycopg/psycopg2-2.0.11.tar.gz
		
		print "PostgreSQL and psycopg2 have been installed\nMoving on to Django\n";
	}

	
			
}

sub install_postgre {
	print "Installing PostgreSQL\n";
	my $com = "apt-get install postgresql";
	my $res = `$com`;

}

sub log_err {
	print @_[0];
	if(!@_[1])
		exit(1);

}

print "Starting program\n";
check_prereqs;
